package news.yp.newsproject.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;

import news.yp.newsproject.R;

public class MyTextView extends android.support.v7.widget.AppCompatTextView {

    private int textColor;
    private int spSize;
    private CharSequence text;
    private Paint mPaint;

    public MyTextView(Context context) {
        this(context,null);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        Log.e("TAG", "解析自定义属性");
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        textColor = typedArray.getColor(R.styleable.MyTextView_color, Color.RED);
        spSize = typedArray.getDimensionPixelSize(R.styleable.MyTextView_textsize, 10);
        text = typedArray.getText(R.styleable.MyTextView_text);
        //不管三七二十一，先回收掉
        typedArray.recycle();
        //设置画笔的大小和颜色
        mPaint = new Paint();
        mPaint.setColor(textColor);
        mPaint.setTextSize(sp2px(spSize));
        mPaint.setAntiAlias(true);

    }

    private float sp2px(int spSize) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, spSize,
                getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //先假设给的是确定的值（也就是match_parent或者是指定的值直接就可以getSize），
        // 下面的if如果能进去，就让他去改变
        int width = MeasureSpec.getSize(widthMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        if (widthMode==MeasureSpec.AT_MOST){ //是wrap_content,需要用画笔来计算
            Rect bounds = new Rect();
            mPaint.getTextBounds((String) text, 0,text.length() ,bounds );
            width = bounds.width()+getPaddingLeft()+getPaddingRight();
        }
        //同样的道理，高度也是这么来搞
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (heightMode==MeasureSpec.AT_MOST){
            Rect bounds = new Rect();
            mPaint.getTextBounds((String) text,0 ,text.length() ,bounds );
            height = bounds.height()+getPaddingTop()+getPaddingBottom();

        }
        setMeasuredDimension(width,height );
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("TAG","调用onDraw方法" );
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        int dy = (int) ((fontMetrics.bottom-fontMetrics.top)/2-fontMetrics.bottom);
        int baseLine = getHeight()/2+dy;
        int x = getPaddingLeft();

        canvas.drawText((String) text,x ,baseLine , mPaint);

    }
}
